
package tilefoneia;

import java.util.ArrayList;
import java.util.Collections;
import java.io.*;

public class Omilias extends Programma {
    private double xreosi = 0;
    private int orioXronouOmilias;
    private int synolXronosOmilias = 0;
    private double timiXreosis = 0.20f;
    private boolean ypervasiOriouXronou;
    private ArrayList<KinisiKlisis> kliseis;
    
    public Omilias(String hmeromEnarxis, int lepta) {
        super(hmeromEnarxis, 1);
        this.orioXronouOmilias = lepta;
        kliseis = new ArrayList<KinisiKlisis>();
    }
    
    public int getArithmoKlisewn() {
        return kliseis.size();
    }
    
    public void addKlisi(String hmerominia, String ora_lepta, int diarkeia) {   
        KinisiKlisis kinKlisis = new KinisiKlisis(hmerominia, ora_lepta, diarkeia, ypologXreosiKlisis(diarkeia));
        kliseis.add(kinKlisis);
    }
    
    public double ypologXreosiKlisis(int diarkeia)  {
        double xreosiOmilias = 0.0f;
        synolXronosOmilias = synolXronosOmilias + diarkeia;
        if (ypervasiOriouXronou) {
            xreosiOmilias = (diarkeia * timiXreosis);
        }
        else
            if (synolXronosOmilias >= orioXronouOmilias) {
                ypervasiOriouXronou = true;
                int xreosimosXronos = synolXronosOmilias - orioXronouOmilias;
                xreosiOmilias = (xreosimosXronos * timiXreosis);
            }
         xreosi = xreosi + xreosiOmilias;
         return xreosiOmilias;
    }
    
    public void taxinomisiKlisewn() {
        System.out.println("\nΓίνεται ΤΑΞΙΝΟΜΗΣΗ των κινήσεων");
        //Sort swap = null;  
        int n = this.kliseis.size();
        int i = n-1;   //last array element

        boolean swapDone = true;  
        while ( (i>=1) && (swapDone) ) {
            swapDone = false;
            for (int j=0;j<=(i-1);j++){
                if (this.kliseis.get(j).getDiarkeia()<this.kliseis.get(j+1).getDiarkeia()){
                           Collections.swap(this.kliseis,j,j+1); 
                    swapDone=true;
                }
            }//end for
            i--;
        }//end while
    }//end taxinomisi
   
    public void ektyposiKlisewn() {
        System.out.println("\nΓίνεται ΕΚΤΥΠΩΣΗ των κινήσεων");
        for (KinisiKlisis klisi : kliseis) 
            System.out.println(klisi);
    }

    public void apothikeysiKlisewn(PrintWriter pw) { 
        System.out.println("\nΓίνεται ΑΠΟΘΗΚΕΥΣΗ των κινήσεων");
        for (KinisiKlisis klisi : kliseis) {
                pw.println(klisi);     
                //System.out.println("PRINT: " + klisi);
        }
    }
}
